package org.csu.mypetstore.service.impl;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.csu.mypetstore.service.AdminCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("adminCatalogService")
public class AdminCatalogServiceImpl implements AdminCatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public void addCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(String categoryId) {
        categoryMapper.deleteById(categoryId);
    }

    @Override
    public Category getCategory(String categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    @Override
    public List<Category> listCategories() {
        return categoryMapper.selectList(null);
    }

    @Override
    public void addProduct(Product product) {
        try {
            productMapper.insert(product);  // 确保调用插入操作
            System.out.println("Product added: " + product);  // 查看是否执行
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to add product");
        }
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void deleteProduct(String productId) {
        productMapper.deleteById(productId);
    }

    @Override
    public Product getProduct(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public List<Product> listProducts() {
        return productMapper.selectList(null);
    }

    @Override
    public int searchProduct(String productId) {
        int count = productMapper.searchProductByProductId(productId);
       return count;
    }

    @Override
    public void publishProduct(String productId) {
        Product product=productMapper.selectById(productId);
        if(product!=null){
//            product.setStatus("PUBLISHED");
            productMapper.updateById(product);
        }
    }

    // 获取所有类别
    @Override
    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    @Override
    public void addItem(Item item) {
        itemMapper.insert(item);
    }

    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    public void deleteItem(String itemId) {
        itemMapper.deleteById(itemId);
    }

    @Override
    public Item getItem(String itemId) {
        return itemMapper.selectById(itemId);
    }

    @Override
    public List<Item> listItems() {
        return itemMapper.selectList(null);
    }

}
